package ModelLayer;

import java.util.ArrayList;
/**
 * The Composition class holds information about a composition.
 * A composition inherits variables from superclass Item.
 * 
 * @author  Mie P., Jakob K., Anna K., Anders A.P., Danny M.P.  
 * @version 1.0
 */
public class Composition extends Item {

    private ArrayList<CompositionLine> compLines;
    private double totalPurchasePrice;

    /**
     * Constructor for objects of class Composition
     * 
     * @param name          Name of the composition.
     * @param description   A description of the composition.
     */
    public Composition(String name, String description) {

        super(name, description);
        compLines = new ArrayList<CompositionLine>();

    }

    /**
     * Method for adding a product with a given quantity to the composition.
     * Creates a CompositionLine object, and adds it to the collection in the Composition object.
     * 
     * @param p             The Product object.
     * @param quantity      The quantity of the item.
     */
    public void addProduct(Product p, int quantity) {

        CompositionLine cl = new CompositionLine(p, quantity);
        compLines.add(cl);
        calculateTotalPurchasePrice();

    }

    /**
     * Calculates the total purchase price of the composition.
     */
    private void calculateTotalPurchasePrice() {

        double total = 0;
        for (CompositionLine cl : compLines) {
            total = total + cl.getLinePrice();
        }
        this.totalPurchasePrice = total;

    }

    /**
     * Removes a CompositionLine object from the collection compLines.
     * 
     * @param   cl  The CompositionLine object to be deleted.  
     */
    public void deleteCompLine(CompositionLine cl) {

        compLines.remove(cl);

    }

    /**
     * Accessor method for compLines.
     * 
     * @return  compLines   an ArrayList of all the composition lines associated to the Composition object.
     */
    public ArrayList<CompositionLine> getCompLines() {

        return compLines;

    }

    /**
     * Accessor method for totalPurchasePrice.
     * 
     * @return  totalPurchasePrice  the calculated value of totalPurchasePrice.
     */
    public double getTotalPurchasePrice() {

        return this.totalPurchasePrice;

    }
}
